<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Will return one text value for specified workspace, language, module, label and id
 * @global JFXConfig $CONFIG
 * @param JFX_Module $module
 * @param string $label
 * @param string $id
 * @param string $lang
 * @return <type>
 */

class JFX_Lang{

    protected $lang = 'en_US';

    public static function getInstance(){
        $USER = JFX::registry('JFX_User');
        if(!isset($USER->langObject)){
            $USER->langObject = new JFX_Lang;
        }
        $LANG = $USER->langObject;
        $LANG->lang = $USER->lang;
        return $LANG;
    }

    /**
     * Will return one text value for specified workspace, language, module, label and id
     * @global JFXConfig $CONFIG
     * @param JFX_Module $module
     * @param string $label
     * @param string $id
     * @param string $lang
     * @return <type>
     */

    public function getContent($module, $label, $id, $lang='', $workspaceid=''){
        $DB = JFX::registry('db');
        $USER = JFX::registry('JFX_User');
        $CONFIG = JFX::registry('config');

        if($lang=='') $lang = $USER->lang;
        if($workspaceid == '') $workspaceid = $USER->workspace;

        $module = $DB->escape($module);
        $key = $DB->escape($label);
        $id = $DB->escape($id);
        $workspaceid = $DB->escape($workspaceid);

        $res = $DB->oneValue($CONFIG->dbprefix.'lang_content', 'v', "module_key = '{$module}' AND label = '{$label}' AND item_id = '{$id}' AND lang = '{$lang}' AND workspace_id = '{$workspaceid}'");

        return $res;
    }

    /**
     * Will create or update a language entry for all or one installed workspace and language
     * @global JFXConfig $CONFIG
     * @param JFX_Module $module
     * @param string $label
     * @param string $id
     * @param string $value
     * @param string $lang
     * @param int $workspaceid
     * @return void
     */

    public function updateContent($module, $label, $id, $value, $lang='', $workspaceid=''){
        $DB = JFX::registry('db');
        $USER = JFX::registry('JFX_User');
        //$DB->showOutput(true);
        //$DB->showErrors(true);
        $CONFIG = JFX::registry('config');



        // Updating all langs and workspaces
        if($lang == '*'){
            $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
            if($workspaceid == '*'){
                // all langs, all workspaces
                $workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");
                foreach($workspaces as $ws){
                    foreach($installedLangs as $lang){
                        $this->updateContent($module, $label, $id, $value, $lang['keyname'], $ws['id']);
                    }
                }
                return;
            }else{
                // all langs, one workspace
                foreach($installedLangs as $lang){
                    $this->updateContent($module, $label, $id, $value, $lang['keyname'], $workspaceid);
                }
                return;
            }
        }else if($workspaceid == '*'){
            // all workspaces, one lang
            if($lang == '') $lang = $this->lang;
            $workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");
            foreach($workspaces as $ws){
                $this->updateContent($module, $label, $id, $value, $lang, $ws['id']);
            }
            return;
        }

        // only updating one workspace and one language
        if($lang=='') $lang = $this->lang;
        if($workspaceid=='') $workspaceid = $USER->workspace;
        $module = $DB->escape($module);
        $label = $DB->escape($label);
        $id = $DB->escape($id);
        $workspaceid = (int) $workspaceid;
        if($DB->countRows($CONFIG->dbprefix.'lang_content', "module_key = '{$module}' AND label = '{$label}' AND item_id = '{$id}' AND lang = '{$lang}' AND workspace_id = '{$workspaceid}'")>0){
            $dbParams = array(
                'v' => $value
            );
            //echo __FILE__.' updating v to '.$value.' where id = '.$id.' and label = '.$label.' and module = '.$module.' and workspace = '.$workspaceid.' and lang = '.$lang;
            $DB->update($CONFIG->dbprefix.'lang_content', $dbParams, "module_key = '{$module}' AND label = '{$label}' AND item_id = '{$id}' AND lang = '{$lang}' AND workspace_id = '{$workspaceid}'");
        }else{
            $dbParams = array(
                'module_key' => $module,
                'label' => $label,
                'item_id' => $id,
                'lang' => $lang,
                'workspace_id' => $workspaceid,
                'v' => $value
            );

            $DB->insert($CONFIG->dbprefix.'lang_content', $dbParams);
        }
        //echo __FILE__.' insertign v to '.$value.' where id = '.$id.' and label = '.$label.' and module = '.$module.' and workspace = '.$workspaceid.' and lang = '.$lang;
        // make sure there is a copy for each installed language
        $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
        foreach($installedLangs as $k=>$newLang){
            if($lang == $newLang) continue;

        
            if($DB->countRows($CONFIG->dbprefix.'lang_content', "module_key = '{$module}' AND label = '{$label}' AND item_id = '{$id}' AND lang = '{$lang}' AND workspace_id = '{$workspaceid}'")==0){
                $dbParams = array(
                    'module_key' => $module,
                    'label' => $label,
                    'item_id' => $id,
                    'workspace_id' => $workspaceid,
                    'lang' => $newLang,
                    'v' => $value
                );
                $DB->insert($CONFIG->dbprefix.'lang_content', $dbParams);
            }
        }
        //$DB->showOutput(false);
        //$DB->showErrors(false);
    }





    public function deleteContent($module, $label, $id, $lang='', $workspaceid=''){
        $DB = JFX::registry('db');
        $USER = JFX::registry('JFX_User');
        //$DB->debug(true);
        //$DB->showErrors(true);
        $CONFIG = JFX::registry('config');

        // Updating all langs and workspaces
        if($lang == '*'){
            $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
            if($workspaceid == '*'){
                // all langs, all workspaces
                $workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");
                foreach($workspaces as $ws){
                    foreach($installedLangs as $lang){
                        $this->deleteContent($module, $label, $id, $lang['keyname'], $ws['id']);
                    }
                }
                return;
            }else{
                // all langs, one workspace
                foreach($installedLangs as $lang){
                    $this->deleteContent($module, $label, $id, $lang['keyname'], $workspaceid);
                }
                return;
            }
        }else if($workspaceid == '*'){
            // all workspaces, one lang
            if($lang == '') $lang = $this->lang;
            $workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");
            foreach($workspaces as $ws){
                $this->deleteContent($module, $label, $id, $lang, $ws['id']);
            }
            return;
        }

        // only updating one workspace and one language
        if($lang=='') $lang = $this->lang;
        if($workspaceid=='') $workspaceid = $USER->workspace;
        $module = $DB->escape($module);
        $label = $DB->escape($label);
        $id = $DB->escape($id);
        $workspaceid = (int) $workspaceid;
        $DB->delete($CONFIG->dbprefix.'lang_content', "module_key = '{$module}' AND label = '{$label}' AND item_id = '{$id}' AND lang = '{$lang}' AND workspace_id = '{$workspaceid}'");

        //$DB->debug(false);
        //$DB->showErrors(false);
    }







    public function get($moduleKey, $label, $lang=''){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        //$DB->debug(true);
        //$DB->showErrors(true);

        if($lang == '') $lang = $this->lang;
        $moduleKey = $DB->escape($moduleKey);
        $label = $DB->escape($label);
        $res = (string) $DB->oneValue($CONFIG->dbprefix.'lang_system', 'v', "module_key = '{$moduleKey}' AND label = '{$label}' AND lang = '{$lang}'");
        
        //$DB->debug(false);
        //$DB->showErrors(false);
        
        return $res;

    }

    public function update($moduleKey, $label, $value, $lang=''){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $moduleKey = $DB->escape($moduleKey);
        $label = $DB->escape($label);
        if($lang == '') $lang = $this->lang;
        //$DB->debug(true);
        //$DB->showErrors(true);

        // update all langs if instructed
        if($lang == '*'){
            $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
            foreach($installedLangs as $lang){
                $this->update($module, $label, $value, $lang['keyname']);
            }
            return;
        }



        if($DB->countRows($CONFIG->dbprefix.'lang_system', "module_key = '{$moduleKey}' AND label = '{$label}' AND lang = '{$lang}'")>0){
            // updating
            $dbParams = array(
                'v' => $value,
                'modified_tstamp' => time()
            );
            $DB->update($CONFIG->dbprefix.'lang_system', $dbParams, "module_key = '{$moduleKey}' AND label = '{$label}' AND lang = '{$lang}'");
        }else{
            // inserting
            $dbParams = array(
                'module_key' => $moduleKey,
                'label' => $label,
                'lang' => $lang,
                'v' => $value
            );
            $DB->insert($CONFIG->dbprefix.'lang_system', $dbParams);
        }
        //$DB->debug(false);
        //$DB->showErrors(false);
    }


    public function delete($moduleKey, $label, $value, $lang=''){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $moduleKey = $DB->escape($moduleKey);
        $label = $DB->escape($label);
        if($lang == '') $lang = $this->lang;
        //$DB->debug(true);
        //$DB->showErrors(true);

        // update all langs if instructed
        if($lang == '*'){
            $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
            foreach($installedLangs as $lang){
                $this->delete($module, $label, $value, $lang['keyname']);
            }
            return;
        }


        $DB->delete($CONFIG->dbprefix.'lang_system', "module_key = '{$moduleKey}' AND label = '{$label}' AND lang = '{$lang}'");

        //$DB->debug(false);
        //$DB->showErrors(false);
    }


}